package com.yaodian100.ec.order.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.order.entity.OrderHistory;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.service.OrderHistoryService;
import com.yaodian100.ec.order.service.OrderService;

public class OrderHistoryServiceImpl extends DomainServiceImpl<OrderHistory> implements OrderHistoryService {
	
	@Autowired
	private OrderService orderService;
	
	@Override
	public List<OrderHistory> getOrderHistory(String orderId) {
		List<OrderHistory> result = new ArrayList<OrderHistory>();
		
		Orders order = orderService.get(orderId);
		if (order != null) {
			OrderHistory currentRecord = new OrderHistory();
			currentRecord.setOrderSt(order.getOrderSt());
			currentRecord.setModifyUser(order.getModifyUser());
			currentRecord.setModifyDateForView(order.getModifyDate());
			result.add(currentRecord);
		}
		
		Map<String, Serializable> attrs = new HashMap<String, Serializable>();
		attrs.put("orderId", orderId);
		List<Object[]> queryResult = getDao().getNameQuery("OrderHistory.getByOrderId", attrs, 0, -1);
		for (Object[] oneQueryRecord: queryResult) {
			
			String orderSt = null;
			Object orderStObject = oneQueryRecord[0];
			if (orderStObject != null) {
				orderSt = StringUtils.trimToNull(orderStObject.toString());
			}
			
			String modifyUserId = null;
			Object modifyUserIdObject = oneQueryRecord[1];
			if (modifyUserIdObject != null) {
				modifyUserId = StringUtils.trimToNull(modifyUserIdObject.toString());
			}
			
			Date modifyDate = (Date) oneQueryRecord[2];
			
			OrderHistory oneHistoryRecord = new OrderHistory();
			oneHistoryRecord.setOrderSt(orderSt);
			oneHistoryRecord.setModifyUser(modifyUserId);
			oneHistoryRecord.setModifyDateForView(modifyDate);
			
			result.add(oneHistoryRecord);
		}
		
		return result;
	}
	
	public OrderService getOrderService() {
		return this.orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

}
